//
//  HDCameraSourceManager.m
//  CreditGroup
//
//  Created by ltl on 15/9/11.
//  Copyright (c) 2015年 JPlay. All rights reserved.
//

#import "MCCameraSourceManager.h"
#import "MCCameraDefault.h"
#import "MCCameraCustom.h"
#import "MCCameraImageRotationHandle.h"
#import "MCCameraImageClipHandle.h"
#import "MCCameraCompressHandle.h"

typedef NS_ENUM(NSInteger, EMCCameraClassType) {
    kMCCameraClassTypeDefault = 0,
    kMCCameraClassTypeCustom,
};

@interface MCCameraSourceManager ()

@property (strong, nonatomic) MCCameraDefault *cameraDefault;
@property (strong, nonatomic) MCCameraCustom  *cameraCustom;
@property (strong, nonatomic) UIImage *returnImage;
@property (assign, nonatomic) EMCImageType type;
@end

@implementation MCCameraSourceManager

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        NSLog(@"HDCameraSourceManager init is success");
        self.rotation = kMCImageRotationTypeNone;
        self.size     = CGSizeZero;
        self.format   = kMCImageFormatDefault;
        self.type     = kMCImageTypeNomal;
    }
    return self;
}

- (void)setIsHaveFrame:(EMCCameraIsHaveFrame)isHaveFrame{
    _isHaveFrame = isHaveFrame;
}

- (void)setIsClip:(EMCCameraIsClip)isClip{
    _isClip = isClip;
}

- (void)setRotation:(EMCImageRotationType)rotation{
    _rotation = rotation;
}

- (void)setSize:(CGSize)size{
    _size = size;
}

- (void)setFormat:(EMCImageFormat)format{
    _format = format;
}

- (void)setQuality:(CGFloat)quality{
    _quality = quality;
}

- (void)setType:(EMCImageType)type{
    _type = type;
}

- (void)setImageData:(NSData *)imageData{
    _imageData = imageData;
}

// ************************************************************拿取 UIImage***************************************************

- (void)callCameraDefaultWithGetImageBlcok:(EMCCameraGetImageBlock)getImageBlock
{
    [self callCameraClassWithType:kMCCameraClassTypeDefault
                    GetImageBlock:getImageBlock];
}

- (void)callCameraCustomWithEventType:(EActionSheetEventType)eventType
                        GetImageBlock:(EMCCameraGetImageBlock)getImageBlock
{
    // 记录点击事件
    if (self.eventBlock)
    {
        self.eventBlock(eventType);
    }
    [self callCameraClassWithType:kMCCameraClassTypeCustom
                    GetImageBlock:getImageBlock];
    [self.cameraCustom callCorrespondingClassByType:eventType];
}

- (void)callCameraClassWithType:(EMCCameraClassType)cameraType
                  GetImageBlock:(EMCCameraGetImageBlock)getImageBlock
{
    __weak MCCameraSourceManager *weakself = self;
    EMCCameraClassType type = cameraType;
    switch (type)
    {
        case kMCCameraClassTypeDefault:
        {
            self.cameraDefault = [[MCCameraDefault alloc] init];
            self.cameraDefault.isHaveFrame = _isHaveFrame;
            self.cameraDefault.isClip = _isClip;
            self.cameraDefault.rotation    = _rotation;
            self.cameraDefault.size        = _size;
            self.cameraDefault.format      = _format;
            self.cameraDefault.quality     = _quality;
            self.cameraDefault.cameraDefaultReturnImageBlock = ^(UIImage *image)
            {
                getImageBlock(image);
            };
            // 记录点击事件
            self.cameraDefault.eventBlock = ^(EActionSheetEventType eventType){
                if (weakself.eventBlock)
                {
                    weakself.eventBlock(eventType);
                }
            };
        }
            break;
        case kMCCameraClassTypeCustom:
        {
            self.cameraCustom = [[MCCameraCustom alloc] init];
            self.cameraCustom.isHaveFrame = _isHaveFrame;
            self.cameraCustom.isClip = _isClip;
            self.cameraCustom.rotation    = _rotation;
            self.cameraCustom.size        = _size;
            self.cameraCustom.format      = _format;
            self.cameraCustom.quality     = _quality;
            self.cameraCustom.cameraCustomReturnImageBlock = ^(UIImage *image)
            {
                getImageBlock(image);
            };
        }
            break;
            
        default:
            break;
    }
}

// ************************************************************拿取 NSData************************************************************

- (void)callCameraDefaultWithGetImageDataBlcok:(EMCCameraGetImageDataBlock)getImageDataBlock{
    [self setType:kMCImageTypeData];
    [self callCameraClassWithType:kMCCameraClassTypeDefault
                GetImageDataBlock:getImageDataBlock];
}

- (void)callCameraCustomWithEventType:(EActionSheetEventType)eventType GetImageDataBlock:(EMCCameraGetImageDataBlock)getImageDataBlock{
    [self setType:kMCImageTypeData];
    [self callCameraClassWithType:kMCCameraClassTypeCustom
                GetImageDataBlock:getImageDataBlock];
}

- (void)callCameraClassWithType:(EMCCameraClassType)cameraType
              GetImageDataBlock:(EMCCameraGetImageDataBlock)getImageDataBlock
{
    __weak MCCameraSourceManager *weakself = self;
    EMCCameraClassType type = cameraType;
    switch (type)
    {
        case kMCCameraClassTypeDefault:
        {
            self.cameraDefault = [[MCCameraDefault alloc] init];
            self.cameraDefault.isHaveFrame = _isHaveFrame;
            self.cameraDefault.rotation    = _rotation;
            self.cameraDefault.size        = _size;
            self.cameraDefault.format      = _format;
            self.cameraDefault.quality     = _quality;
            self.cameraDefault.type        = _type;
            self.cameraDefault.cameraDefaultReturnImageDataBlock = ^(NSData *imageData)
            {
                [weakself setImageData:imageData];
                getImageDataBlock(imageData);
            };
            // 记录点击事件
            self.cameraDefault.eventBlock = ^(EActionSheetEventType eventType){
                if (weakself.eventBlock)
                {
                    weakself.eventBlock(eventType);
                }
            };
        }
            break;
        case kMCCameraClassTypeCustom:
        {
            self.cameraCustom = [[MCCameraCustom alloc] init];
            self.cameraCustom.isHaveFrame = _isHaveFrame;
            self.cameraCustom.rotation    = _rotation;
            self.cameraCustom.size        = _size;
            self.cameraCustom.format      = _format;
            self.cameraCustom.quality     = _quality;
            self.cameraCustom.type        = _type;
            self.cameraCustom.cameraCustomReturnImageDataBlock = ^(NSData *imageData)
            {
                [weakself setImageData:imageData];
                getImageDataBlock(imageData);
            };
        }
            break;
            
        default:
            break;
    }
}



- (void)dealloc{
    NSLog(@"MCCameraSourceManager = nil");
}

@end
